/*
 * JTalkerClientPluginsInfo.java
 *
 * Created on 9. Dezember 2005, 11:32
 */

package info.chrm.jtalker;

/**
 *
 * @author  chrmac@gmx.de
 */
public class JTalkerClientPluginsInfo extends javax.swing.JFrame {
    private JTalkerClientFront parent = null;
    private JTalkerClientPluginLoader myLoader = null;
    
    /** Creates new form JTalkerClientPluginsInfo */
    public JTalkerClientPluginsInfo(JTalkerClientFront _parent) {
        this.parent = _parent;
        
        this.initComponents();
        //this.setPreferredSize(new java.awt.Dimension(400, 200));
        //
        //this.updatePluginList(null);        
    }

    /** Updates the plugin list */
    public boolean updatePluginList(String _searchURL) {
        final String [] tmpListItems;
       
        try { 
            if(_searchURL != null) {
                this.myLoader = this.parent.getPluginLoader();
                //this.myLoader = new JTalkerClientPluginLoader(this.parent);
                this.myLoader.initPlugins(_searchURL);
                tmpListItems = this.myLoader.getPluginNames();
            } else {
                this.myLoader = this.parent.getPluginLoader();
                tmpListItems = this.myLoader.getPluginNames();
            }

            if(tmpListItems == null ||
               tmpListItems.length == 0) {
               javax.swing.JOptionPane.showMessageDialog(this, 
						      "No plugins found in:\n" + _searchURL, 
						      "Scanning directory", 
						      javax.swing.JOptionPane.INFORMATION_MESSAGE);
               this.setVisible(false);
               return false; 
            }
     
            jListAllPlugIns.setModel(new javax.swing.AbstractListModel() {
                String[] strings = tmpListItems;
                public int getSize() { 
                    if(strings == null)
                        return 0;
                    return strings.length; 
                }
                public Object getElementAt(int i) { 
                    if(strings[i] != null)
                        return strings[i]; 
                    else
                        return "---";
                }
            });
        } catch (Exception _e) {
            System.err.println("*** Error while reloading plugins: " + _e.toString());
            return false;
        } 

        return true;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanelButtons = new javax.swing.JPanel();
        jButtonOK = new javax.swing.JButton();
        jScrollPaneAllPlugIns = new javax.swing.JScrollPane();
        jListAllPlugIns = new javax.swing.JList();
        jPanelMainContent = new javax.swing.JPanel();
        jLabelNAME = new javax.swing.JLabel();
        jLabelCLASS = new javax.swing.JLabel();
        jLabelDESC = new javax.swing.JLabel();
        jLabelName = new javax.swing.JLabel();
        jLabelClass = new javax.swing.JLabel();
        jScrollPaneDesc = new javax.swing.JScrollPane();
        jTextAreaDesc = new javax.swing.JTextArea();
        jScrollPaneMethods = new javax.swing.JScrollPane();
        jListMethods = new javax.swing.JList();
        jLabelIcon = new javax.swing.JLabel();
        jCheckBoxShortcut = new javax.swing.JCheckBox();

        setTitle("PlugIn Overview");
        setName("JFramePluginsInfo");
        setResizable(false);
        jPanelButtons.setPreferredSize(new java.awt.Dimension(545, 40));
        jButtonOK.setText("OK");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jPanelButtons.add(jButtonOK);
        getContentPane().add(jPanelButtons, java.awt.BorderLayout.SOUTH);        
        jListAllPlugIns.setPreferredSize(new java.awt.Dimension(150, 100));
        jListAllPlugIns.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListAllPlugInsValueChanged(evt);
            }
        });

        jScrollPaneAllPlugIns.setViewportView(jListAllPlugIns);

        getContentPane().add(jScrollPaneAllPlugIns, java.awt.BorderLayout.WEST);

        jPanelMainContent.setLayout(null);

        jPanelMainContent.setPreferredSize(new java.awt.Dimension(80, 320));
        jLabelNAME.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelNAME.setText("Name:");
        jLabelNAME.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanelMainContent.add(jLabelNAME);
        jLabelNAME.setBounds(0, 10, 80, 30);

        jLabelCLASS.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelCLASS.setText("Main-Class:");
        jLabelCLASS.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanelMainContent.add(jLabelCLASS);
        jLabelCLASS.setBounds(0, 40, 80, 30);

        jLabelDESC.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelDESC.setText("Description:");
        jLabelDESC.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanelMainContent.add(jLabelDESC);
        jLabelDESC.setBounds(0, 70, 90, 30);

        jLabelName.setText("");
        jLabelName.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanelMainContent.add(jLabelName);
        jLabelName.setBounds(90, 10, 170, 30);

        jLabelClass.setText("");
        jLabelClass.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanelMainContent.add(jLabelClass);
        jLabelClass.setBounds(90, 40, 170, 30);

        jScrollPaneDesc.setAutoscrolls(true);
        jTextAreaDesc.setColumns(20);
        jTextAreaDesc.setEditable(false);
        jTextAreaDesc.setLineWrap(true);
        jTextAreaDesc.setRows(5);
        jTextAreaDesc.setText("");
        jTextAreaDesc.setWrapStyleWord(true);
        jScrollPaneDesc.setViewportView(jTextAreaDesc);

        jPanelMainContent.add(jScrollPaneDesc);
        jScrollPaneDesc.setBounds(100, 70, 280, 50);

        jScrollPaneMethods.setBorder(javax.swing.BorderFactory.createTitledBorder("Methods"));       
        jScrollPaneMethods.setViewportView(jListMethods);

        jPanelMainContent.add(jScrollPaneMethods);
        jScrollPaneMethods.setBounds(10, 130, 370, 140);

        jLabelIcon.setText("NO ICON");
        jLabelIcon.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanelMainContent.add(jLabelIcon);
        jLabelIcon.setBounds(270, 10, 60, 50);

        jCheckBoxShortcut.setText("Create a shortcut for this plugin in the main window");
        jCheckBoxShortcut.setToolTipText("Creates a button in JTalkers toolbar");
        jCheckBoxShortcut.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBoxShortcut.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanelMainContent.add(jCheckBoxShortcut);
        jCheckBoxShortcut.setBounds(10, 280, 370, 15);
        jCheckBoxShortcut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxShortcutActionPerformed(evt);
            }
        });

        getContentPane().add(jPanelMainContent, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Embeding plugin was checked */
    private void jCheckBoxShortcutActionPerformed(java.awt.event.ActionEvent evt) {
       String selectedPlugin = (String)this.jListAllPlugIns.getSelectedValue();

       if(this.jCheckBoxShortcut.isSelected()) {
            this.parent.theClient.addPluginShortcut(selectedPlugin);            
       } else {            
            this.removeEmbeddedStatusFor(selectedPlugin);
       } 

       this.parent.updatePluginShortcuts();
    }

    /** Ok was clicked */
    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {
       String selectedPlugin = (String)this.jListAllPlugIns.getSelectedValue();
       JTalkerClientPlugInInterface tmpPlugin = this.myLoader.getPluginByName(selectedPlugin);
       
       this.dispose();
    }

    private void jListAllPlugInsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListAllPlugInsValueChanged
        String selectedPlugin = (String)this.jListAllPlugIns.getSelectedValue();
        JTalkerClientPlugInInterface tmpPlugin = this.myLoader.getPluginByName(selectedPlugin);

        if(tmpPlugin == null)
            return;
        
        this.jLabelName.setText(selectedPlugin);
        
        this.jLabelIcon.setText("");
        this.jLabelIcon.setOpaque(true);
        this.jLabelIcon.setIcon(tmpPlugin.getIcon());
        
        this.jLabelClass.setText(tmpPlugin.getClass().getName());
        
        this.jTextAreaDesc.setText(tmpPlugin.getDescription());

        java.lang.reflect.Method [] allMethods = tmpPlugin.getCommandMethods();
        final String [] tmpMethods = new String[allMethods.length];

        for( int i = 0; i < allMethods.length; i++ ) {
            tmpMethods[i] = allMethods[i].getName();
        }
        
        jListMethods.setModel(new javax.swing.AbstractListModel() {
            String[] strings = tmpMethods;
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });

        if(this.isPluginEmbedded(selectedPlugin)) {
            this.jCheckBoxShortcut.setSelected(true);
        } else
            this.jCheckBoxShortcut.setSelected(false);
        
    }//GEN-LAST:event_jListAllPlugInsValueChanged

    /** Checks, if selected plugin is embedded in the clients toolbar */
    private boolean isPluginEmbedded(String _pluginName) {        
        return this.parent.theClient.isPluginShortcut(_pluginName);
    }

    /** Removes an embedded plugin from the list */
    private boolean removeEmbeddedStatusFor(String _pluginName) {
        return this.parent.theClient.removePluginShortcut(_pluginName);
    }
    
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JTalkerClientPluginsInfo().setVisible(true);
            }
        });
    }*/
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonOK;
    private javax.swing.JCheckBox jCheckBoxShortcut;
    private javax.swing.JLabel jLabelCLASS;
    private javax.swing.JLabel jLabelClass;
    private javax.swing.JLabel jLabelDESC;
    private javax.swing.JLabel jLabelIcon;
    private javax.swing.JLabel jLabelNAME;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JList jListAllPlugIns;
    private javax.swing.JList jListMethods;
    private javax.swing.JPanel jPanelButtons;
    private javax.swing.JPanel jPanelMainContent;
    private javax.swing.JScrollPane jScrollPaneAllPlugIns;
    private javax.swing.JScrollPane jScrollPaneDesc;
    private javax.swing.JScrollPane jScrollPaneMethods;
    private javax.swing.JTextArea jTextAreaDesc;
    // End of variables declaration//GEN-END:variables
    
}
